/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-13 14:00:10
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-10-08 05:42:01
 */

import {
  employeePage,
  employeeList,
  employeeAdd,
  employeeEdit,
  employeeDetail,
} from 'services/employee';

export default {
  namespace: 'employeeList',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    familyList: [],
    employeeList: [],
    detailData: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(employeePage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },

    *employeeList({ payload, callback }, { call, put }) {
      const response = yield call(employeeList, payload);
      yield put({
        type: 'saveEmployeeList',
        payload: response,
      });
      if (callback) callback(response);
    },

    *employeeDetail({ payload }, { call, put }) {
      const response = yield call(employeeDetail, payload);
      yield put({
        type: 'saveEmployeeDetail',
        payload: response,
      });
    },

    *clearEmployeeDetail({ payload }, { call, put }) {
      yield put({
        type: 'saveClearEmployeeDetail',
      });
    },

    *employeeAdd({ payload, callback }, { call, put }) {
      const response = yield call(employeeAdd, payload);
      yield put({
        type: 'saveEmployeeAdd',
        payload: response,
      });
      callback(response);
    },

    *employeeEdit({ payload, callback }, { call, put }) {
      const response = yield call(employeeEdit, payload);
      yield put({
        type: 'saveEmployeeAdd',
        payload: response,
      });
      callback(response);
    },
    // 新增员工--家属新增
    *addFamilyList({ payload }, { call, put }) {
      yield put({
        type: 'saveAddFamilyList',
      });
    },
    // 新增员工--家属修改
    *editFamilyList({ payload }, { call, put }) {
      yield put({
        type: 'saveEditFamilyList',
        payload,
      });
    },
    // 新增员工--家属删除
    *deleteFamilyList({ payload }, { call, put }) {
      yield put({
        type: 'saveDeleteFamilyList',
        payload,
      });
    },

    // 新增员工--家属删除
    *deleteFamilyListAll({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveDeleteFamilyListAll',
        payload,
      });
      if (callback) callback();
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { employeeList, pageIndex, pageSize, recordCount },
      } = action.payload;
      if (!(employeeList instanceof Array)) {
        return;
      }
      const newList = employeeList.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },

    saveEmployeeList(state, action) {
      const { data } = action.payload;
      if (!(data instanceof Array)) {
        return {
          ...state,
        };
      }
      const newList = data.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        employeeList: data,
      };
    },

    saveEmployeeDetail(state, action) {
      const {
        data,
        data: { familyList },
      } = action.payload;

      const newList = familyList.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        v.type = 'edit';
        return v;
      });
      return {
        ...state,
        detailData: data,
        familyList: newList,
      };
    },

    saveClearEmployeeDetail(state, action) {
      return {
        ...state,
        detailData: {},
      };
    },

    saveAddFamilyList(state, action) {
      const familyList = state.familyList;
      familyList.push({
        key: familyList.length + 1,
        type: 'add',
        address: '',
        identityNo: '',
        identityType: '',
        marriageStatus: '',
        mobile: '',
        name: '',
        relationType: '',
        sex: '',
      });

      return {
        ...state,
        familyList,
      };
    },
    saveEditFamilyList(state, action) {
      const familyList = state.familyList;
      const newList = familyList.map(val => {
        const v = val;
        let v1 = v;
        if (v.key === action.payload.key) {
          v1 = { ...v, ...action.payload };
        }
        return v1;
      });
      return {
        ...state,
        familyList: newList,
      };
    },

    saveDeleteFamilyList(state, action) {
      const familyList = state.familyList;
      const newList = familyList.filter(val => {
        const v = val;
        if (v.key != action.payload.key) {
          return v;
        }
      });
      return {
        ...state,
        familyList: newList,
      };
    },
    saveDeleteFamilyListAll(state, action) {
      return {
        ...state,
        familyList: [],
      };
    },
  },
};
